package com.example.demo.service;

import com.example.demo.domain.Announcement;
import com.example.demo.domain.repository.AnnouncementRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: demo
 * @description: 公告服务类
 * @author: wangze
 * @create: 2020-05-20 17:05
 **/
@Service
public class AnnouncementService {
    @Autowired
    private AnnouncementRepository announcementRepository;

    /**
    * @Description: 获取全部公告
    * @Param:  无
    * @return:  全部公告列表
    * @Author: wangze
    */
    public List<Announcement> getAllAnnouncement(){
        return announcementRepository.getAllByIdIsNotNullOrderByDateDesc();
    }

    /**
    * @Description: 搜索公告
    * @Param: content: 搜索内容
    * @return: 特定公告列表
    * @Author: wangze
    */
    public List<Announcement> searchAnnouncement(String content){
        return announcementRepository.findByIntroductionContainingOrderByDateDesc(content);
    }

    /**
    * @Description: 添加公告
    * @Param:  announcement: 公告实体
    * @return:  公告实体
    * @Author: wangze
    */
    public Announcement addAnnouncement(Announcement announcement){
        return announcementRepository.save(announcement);
    }

    /**
     * @Description: 删除公告
     * @Param:  id: 公告ID
     * @return:  无
     * @Author: wangze
     */
    public void deleteAnnouncement(Integer id){
        announcementRepository.deleteAnnouncementById(id);
    }

    /**
     * @Description: 修改公告
     * @Param:  announcement: 公告实体
     * @return:  公告实体
     * @Author: wangze
     */
    public Announcement updateAnnouncement(Announcement announcement){
        return announcementRepository.save(announcement);
    }

    /**
    * @Description: 获取公告详情
    * @Param:  id: 公告ID
    * @return:  公告实体
    * @Author: wangze
    */
    public Announcement getAnnouncement(Integer id){
        return announcementRepository.findAnnouncementById(id);
    }
}
